package com.wengzw.gateway.service;

import com.wengzw.gateway.entity.AuthUserDetails;
import com.wengzw.gateway.utils.MyPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

@Service
public class SecurityUserDetailService implements ReactiveUserDetailsService {

    @Autowired
    private UserService userService;

    @Override
    public Mono<UserDetails> findByUsername(String username) {
        try {
            AuthUserDetails userByUser = userService.getUserByUser(username);
            //需要通过用户名查找密码，并进行校验
            UserDetails user =  User.withUsername(username).password(userByUser.getPassword())
                                .roles("admin", "super").authorities("admin").build();
            return Mono.just(user);
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
